#!/usr/bin/env python
#
# creates a set of patch tarballs for uploading to gentoo servers
#

import os
import glob
import shutil
import sys

# patch revision numbers
# bump these numbers if you modify a patch in the patch set.
ver_rev = {
        '2.2.3':    0,
        '2.3.5':    1,
        '2.3.6':    7,
        '2.4.3':    4,
        '2.4.4':   12,
        '2.4.6':    0,
        '2.5':      2,
        '2.5.1':    3,
        '2.5.2':    8,
        '2.5.4':    3,
        '2.6':      2,
        '2.6.2':    5,
        '2.6.3':    0,
        '2.6.4':    1,
        '2.6.5':    6,
        '2.6.6':    2,
        '2.6.7':    1,
        '2.7_pre':  9,
        '2.7':      1,
        '2.7.1':    2,
        '2.7.2':    1,
        '3.0':      1,
        '3.0.1':    1,
        '3.1':      0,
        '3.1.1':    5,
        '3.1.2':    7,
        '3.1.3':    1,
        '3.1.4':    1,
        '3.2_pre': 12,
        '3.2':      0,
        '3.2.1':    0,
        '3.2.2':    1,
}

# find all directories that start with numbers
for dirname in glob.glob('[0-9]*'):
    if dirname == '.svn':
        continue

    if dirname not in ver_rev:
        print('!! %s is not a recognised version. Skipping.' % dirname)
        continue

    # construct tarball name (add -r if revision is not 0)
    tar = 'python-gentoo-patches-%s.tar.bz2'
    ver = dirname
    rev = ver_rev[ver]
    if rev > 0:
        ver = ver + '-r%d' % rev

    tar = tar % ver

    rc = os.system('tar -cjf %s --exclude .svn %s' % (tar, dirname))
    if rc != 0:
        print('!! failed to create tarball: %s' % tar)
        sys.exit(1)
    else:
        print(' * Created %s' % tar)
